package basics.jvm;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <a href="">
 * </a>
 *
 * <p><span color="#87CEEB">描述:</span>
 * 测试 JVM的四种引用
 *
 * @author or2
 * @date 2021年09月25日 时间: 16:42
 */
public class ReferenceTest {
    public static void main(String[] args) throws InterruptedException {
        testSoftReference();
        testWeakReference();
        /*最弱的幽灵引用, 有兴趣可以自测一下*/
//        testPhantomRefence();
    }

    /**
     * 软链接 - 排行③ - 当JVM空间不足时优先回收<br/>
     * 推荐参数: <span color="green">-Xmx16m -Xms16m</span> </br>
     * <li>我的机器正好回收一半左右
     * @throws InterruptedException
     */
    public static void testSoftReference() throws InterruptedException {
        List<SoftReference<User1M>> list = new ArrayList<>();
        /*被回收时检测*/
        ReferenceQueue<User1M> referenceQueue = new ReferenceQueue<>();

        int times = 10;
        for (int i = 0; i < times; i++) {
            SoftReference<User1M> softReference = new SoftReference<>(new User1M(i + "-soft"), referenceQueue);

            list.add(softReference);
        }

        System.gc();
        TimeUnit.SECONDS.sleep(10);

        for (int i = 0; i < times; i++) {
            Reference<? extends User1M> reference = referenceQueue.poll();
            System.out.println(reference == null ? "null" : reference.get() + "-soft-" + i);
        }

        for (SoftReference<User1M> softReference : list)
            System.out.println(softReference.get());
    }

    /**
     * 弱链接 - gc时会被回收
     * @throws InterruptedException
     */
    public static void testWeakReference() throws InterruptedException {
        List<WeakReference<User1M>> list = new ArrayList<>();
        /*被回收时检测*/
        ReferenceQueue<User1M> referenceQueue = new ReferenceQueue<>();

        int times = 10;
        for (int i = 0; i < times; i++) {
            WeakReference<User1M> softReference = new WeakReference<>(new User1M(i + "-weak"), referenceQueue);

            list.add(softReference);
        }

        /*开启的话, weak便会被回收*/
        System.gc();
        TimeUnit.SECONDS.sleep(1);

        for (int i = 0; i < times; i++) {
            Reference<? extends User1M> reference = referenceQueue.poll();
            System.out.println(reference == null ? "null" : reference.get() + "-weak-" + i);
        }

        for (WeakReference<User1M> weakReference : list)
            System.out.println(weakReference.get());
    }

    /**
     * 1KB大小左右的用户
     */
    private static class User1K {
        private byte[] bytes = new byte[1024];

        private String name;

        public User1K(String name) {
            this.name = name;
        }

        public byte[] getBytes() {
            return bytes;
        }

        public void setBytes(byte[] bytes) {
            this.bytes = bytes;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 1MB大小左右的用户类
     */
    private static class User1M {
        private byte[] bytes = new byte[1024 * 1024];

        private String name;

        public User1M(String name) {
            this.name = name;
        }

        public byte[] getBytes() {
            return bytes;
        }

        public void setBytes(byte[] bytes) {
            this.bytes = bytes;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User1M{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

}
